package cn.edu.sdjzu.knoverse.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.edu.sdjzu.knoverse.model.entity.kg.ComponentNode;
import cn.edu.sdjzu.knoverse.model.entity.kg.FileNode;
import cn.edu.sdjzu.knoverse.model.entity.kg.KnowledgeNode;
import cn.edu.sdjzu.knoverse.model.vo.kg.FileInfoVo;
import cn.edu.sdjzu.knoverse.model.vo.result.Result;
import cn.edu.sdjzu.knoverse.model.vo.result.ResultCodeEnum;
import cn.edu.sdjzu.knoverse.service.ComponentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/component")
@RequiredArgsConstructor
@Tag(name = "组件管理", description = "组件相关接口")
public class ComponentController {

    private final ComponentService componentService;
    
    @PostMapping("/case/{caseId}")
    @SaCheckPermission("component::add")
    @Operation(summary = "创建组件")
    public Result<ComponentNode> createComponent(
            @PathVariable Long caseId,
            @RequestBody ComponentNode component) {
        return Result.success(componentService.createComponent(component, caseId), ResultCodeEnum.ADD_SUCCESS);
    }

    @PutMapping("/{id}")
    @SaCheckPermission("component::update")
    @Operation(summary = "更新组件")
    public Result<ComponentNode> updateComponent(
            @PathVariable Long id, 
            @RequestBody ComponentNode component) {
        component.setNodeId(id);
        return Result.success(componentService.updateComponent(component), ResultCodeEnum.UPDATE_SUCCESS);
    }

    @DeleteMapping("/{id}")
    @SaCheckPermission("component::delete")
    @Operation(summary = "删除组件")
    public Result<Object> deleteComponent(@PathVariable Long id) {
        componentService.deleteComponent(id);
        return Result.success(null, ResultCodeEnum.DELETE_SUCCESS);
    }

    @GetMapping("/{id}")
    @SaCheckPermission("component::detail")
    @Operation(summary = "获取组件详情")
    public Result<ComponentNode> getComponent(@PathVariable Long id) {
        return Result.success(componentService.getComponent(id));
    }
    
    @GetMapping("/case/{caseId}")
    @SaCheckPermission("component::list")
    @Operation(summary = "获取案例的所有组件")
    public Result<Set<ComponentNode>> getComponentsByCase(@PathVariable Long caseId) {
        return Result.success(componentService.getComponentsByCase(caseId));
    }
    
    @PutMapping("/{componentId}/files")
    @SaCheckPermission("component::update-files")
    @Operation(summary = "更新组件的附件文件列表")
    public Result<Object> updateComponentFiles(
            @PathVariable Long componentId,
            @RequestBody List<Long> fileIds) {
        componentService.updateComponentFiles(componentId, fileIds);
        return Result.success(null, ResultCodeEnum.UPDATE_SUCCESS);
    }
    
    @GetMapping("/{componentId}/files")
    @SaCheckPermission("component::files-detail")
    @Operation(summary = "获取组件的附件文件列表")
    public Result<Set<FileInfoVo>> getComponentFiles(@PathVariable Long componentId) {
        return Result.success(componentService.getComponentFilesWithDetails(componentId));
    }
    
    @PutMapping("/{componentId}/knowledge")
    @SaCheckPermission("component::update-knowledge")
    @Operation(summary = "更新组件关联的知识点列表")
    public Result<Object> updateRelatedKnowledge(
            @PathVariable Long componentId,
            @RequestBody List<Long> knowledgeIds) {
        componentService.updateRelatedKnowledge(componentId, knowledgeIds);
        return Result.success(null, ResultCodeEnum.UPDATE_SUCCESS);
    }
    
    @GetMapping("/{componentId}/knowledge")
    @SaCheckPermission("component::knowledge-detail")
    @Operation(summary = "获取组件关联的知识点列表")
    public Result<Set<KnowledgeNode>> getRelatedKnowledge(@PathVariable Long componentId) {
        return Result.success(componentService.getRelatedKnowledge(componentId));
    }
    
    @GetMapping("/knowledge/{knowledgeId}")
    @SaCheckPermission("component::list-by-knowledge")
    @Operation(summary = "获取知识点关联的所有组件")
    public Result<Set<ComponentNode>> getComponentsByKnowledge(@PathVariable Long knowledgeId) {
        return Result.success(componentService.getComponentsByKnowledge(knowledgeId));
    }
} 